<template>
  <div class="search">
    <el-autocomplete
      v-model="hosname"
      :fetch-suggestions="fetchData"
      :trigger-on-focus="false"
      clearable
      placeholder="请你输入医院名称"
      @select="goDetail"
    />
    <el-button type="primary" :icon="Search">搜索</el-button>
  </div>
</template>

<script setup lang="ts">
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { reqHospitalInfo } from '@/api/home/index'
import type { HospitalInfo } from '@/api/home/type'
import { useRouter } from "vue-router";

let hosname = ref<string>('');
const fetchData = async (keyword: string, cb: any) => {
  let res: HospitalInfo = await reqHospitalInfo(keyword)
  let showData = res.data.map(item => {
    return {
      value: item.hosname,
      hoscode: item.hoscode
    }
  })
  cb(showData)
}

let $router = useRouter();
const goDetail = (item: object) => {
  $router.push({ 
    path: "/hospital/register",
    query: {
      hoscode: item.hoscode
    }
  });
};
</script>

<style scoped lang="scss">
.search {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 20px 0;

  //深度选择器:
  // >>>    原生
  // /deep/ less
  // ::v-deep  sass
  ::v-deep(.el-input__wrapper) {
    width: 600px;
    margin-right: 10px;
  }
}
</style>